/**
 * @description 用户关系的controller
 * @author malin
 */

const { getUsersByFollwers, addFollower, removeFollower, getFollowersByUser } = require('../services/user-relation')
const { SuccessModel, ErrorModel } = require('../model/ResModel')
const { addFollowerFailInfo, deleteFollowerFailInfo } = require('../model/ErrorInfo')

/**
 * 根据userId获取粉丝列表
 * @param {number} userId 
 */
async function getFans(userId) {
    // 调用service
    const { count, userList } = await getUsersByFollwers(userId)
    return new SuccessModel({
        count,
        fansList: userList
    })
}

/**
 * 获取用户关注的人
 * @param {number} userId 
 */
async function getFollowers(userId) {
    // service
    const { count, userList } = await getFollowersByUser(userId)
    return new SuccessModel({
        count,
        followersList: userList
    })
}

/**
 * 关注
 * @param {number} myUserId 关注者
 * @param {number} currUserId 被关注者
 */
async function follow(myUserId, currUserId) {
    // service
    try {
        await addFollower(myUserId, currUserId)
        return new SuccessModel()
    } catch(err) {
        console.error(err.message, err.stack)
        return new ErrorModel(addFollowerFailInfo)
    }
}

/**
 * 取消关注
 * @param {number} myUserId 
 * @param {number} currUserId 
 */
async function unFollow(myUserId, currUserId) {
    // service
    try {
        await removeFollower(myUserId, currUserId)
        return new SuccessModel()
    } catch(err) {
        console.error(err.message. err.stack)
        return new ErrorModel(deleteFollowerFailInfo)
    }
}

module.exports = {
    getFans,
    follow,
    unFollow,
    getFollowers
}
